//package com.plian.system.service.om.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.plian.Tools.CollectionUtil;
//import com.plian.Tools.DateUtil;
//import com.plian.Tools.TokenUtil;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.om.SpecialFormType;
//import com.plian.system.dao.sys.orgzation.OrgzationDao;
//import com.plian.system.entity.fr.bank.BankCurrentHistory;
//import com.plian.system.entity.fr.bulletin.NewBulletin;
//import com.plian.system.entity.om.FillPointsRecord;
//import com.plian.system.entity.om.module.ModuleConfig;
//import com.plian.system.entity.om.monitor.MonitorResult;
//import com.plian.system.entity.sys.Message;
//import com.plian.system.mapper.om.FillPointsRecordMapper;
//import com.plian.system.model.ListResult;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.fr.bank.IBankCurrentHistoryService;
//import com.plian.system.service.fr.bulletin.INewBulletinService;
//import com.plian.system.service.om.IFillPointsRecordService;
//import com.plian.system.service.om.module.IModuleConfigService;
//import com.plian.system.service.om.monitor.IMonitorResultService;
//import com.plian.system.service.sys.message.IMessageService;
//import com.plian.system.service.sys.orgzation.OrgzationService;
//import com.plian.system.vo.om.FillAssessmentVO;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.core.env.Environment;
//import org.springframework.stereotype.Service;
//
//import java.text.SimpleDateFormat;
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * @author panqb
// * @version 1.0
// * @description:
// * @date 2022/3/28 下午4:05
// */
//@Service
//@AllArgsConstructor
//@Slf4j
//public class FillPointsRecordServiceImpl extends BaseServiceImpl<FillPointsRecordMapper, FillPointsRecord> implements IFillPointsRecordService {
//
//    OrgzationService orgzationService;
//
//    OrgzationDao orgzationDao;
//
//    Environment environment;
//
//    INewBulletinService newBulletinService;
//
//    IMessageService messageService;
//
//    IBankCurrentHistoryService bankCurrentHistoryService;
//
//    IModuleConfigService moduleConfigService;
//
//    IMonitorResultService monitorResultService;
//
//    FillPointsRecordMapper fillPointsRecordMapper;
//
//    @Override
//    public void autoDeductPoints() {
//        try{
//            List<PageData> resultList = orgzationDao.findGZWUnits();
//            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
//            SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
//            Calendar calendarNow=Calendar.getInstance();
//            Date dateNow=new Date();
//            calendarNow.setTime(dateNow);
//            int year=calendarNow.get(Calendar.YEAR);
//            int month=calendarNow.get(Calendar.MONTH);
//            Calendar thisMonthFirstCal=Calendar.getInstance();
//            thisMonthFirstCal.set(year,month,1);
//            thisMonthFirstCal.add(Calendar.DATE,-1);
//            String lastMonthLastDate=sdf2.format(thisMonthFirstCal.getTime());
//            String lastMonth=sdf.format(thisMonthFirstCal.getTime());
//            String nowDate=sdf2.format(calendarNow.getTime());
//            calendarNow.add(Calendar.MONTH,-1);
//            String ym=sdf.format(calendarNow.getTime());
//
//            QueryWrapper queryWrapper=new QueryWrapper();
//            queryWrapper.eq("status","3");
//            queryWrapper.eq("report_time",lastMonth+"-01");
//
//            List<NewBulletin> bulletinList=newBulletinService.list(queryWrapper);
//            List<String> bulletOrgIds=bulletinList.stream().map(NewBulletin::getOrgId).collect(Collectors.toList());
//            //产权登记扣分
//            QueryWrapper messageQueryWrapper=new QueryWrapper();
//            messageQueryWrapper.eq("form_type","2");
//            //messageQueryWrapper.eq("message_time",lastMonthLastDate);
//            messageQueryWrapper.eq("message_time",nowDate);
//            List<Message> messageList=messageService.list(messageQueryWrapper);
//            List<String> messageOrgList=messageList.stream().map(record->record.getOrgId()).distinct().collect(Collectors.toList());
//            List<PageData> orgAll=orgzationDao.list(null);
//            List<PageData> finalList=new ArrayList<>();
//            //银行资金管理扣分
//            QueryWrapper bankQueryWrapper=new QueryWrapper();
//            bankQueryWrapper.ge("create_time",lastMonthLastDate+" 00:00:00");
//            bankQueryWrapper.le("create_time",lastMonthLastDate+" 23:59:59");
//            List<BankCurrentHistory> bankCurrentHistoryList= bankCurrentHistoryService.list(bankQueryWrapper);
//            List<String> bankOrgList=bankCurrentHistoryList.stream().map(BankCurrentHistory::getOrgId).distinct().collect(Collectors.toList());
//            for(PageData p:resultList){
//                String orgId=p.getString("id");
//                List<PageData> tempList=new ArrayList<>();
//                orgzationService.getAllChildrenNoTreeByPid(orgId,orgAll,tempList);
//                List<String> orgIds=tempList.stream().map(record->record.getString("orgId")).collect(Collectors.toList());
//                List<String> compareList=orgIds.stream().filter(record->!bulletOrgIds.contains(record)).collect(Collectors.toList());
//                List<String> propertyCompareList=orgIds.stream().filter(record->!messageOrgList.contains(record)).collect(Collectors.toList());
//                List<String> bankCompareList=orgIds.stream().filter(record->!bankOrgList.contains(record)).collect(Collectors.toList());
//                if(compareList.size()>3){
//                    p.put("remainNum",compareList.size());
//                    finalList.add(p);
//                    FillPointsRecord fillPointsRecord=new FillPointsRecord();
//                    fillPointsRecord.setType("3");
//                    fillPointsRecord.setScore(SpecialFormType.ENTERPRISE_BULLETIN_DEDUCT_SCORE);
//                    fillPointsRecord.setOrgId(orgId);
//                    fillPointsRecord.setCreateTime(dateNow);
//                    fillPointsRecord.setModuleName("企业快报");
//                    fillPointsRecord.setCheckMonth(ym);
//                    fillPointsRecord.setRemark("存在"+compareList.size()+"家以上子企业无数据");
//                    fillPointsRecord.setDeductScoreDept("系统");
//                    save(fillPointsRecord);
//                }
//                if(propertyCompareList.size()>0){
//                    p.put("remainNum",propertyCompareList.size());
//                    finalList.add(p);
//                    FillPointsRecord fillPointsRecord=new FillPointsRecord();
//                    fillPointsRecord.setType("3");
//                    fillPointsRecord.setScore(SpecialFormType.PROPERTY_REGISTER_DEDUCT_SCORE);
//                    fillPointsRecord.setOrgId(orgId);
//                    fillPointsRecord.setCreateTime(dateNow);
//                    fillPointsRecord.setModuleName("改革与产权-产权登记");
//                    fillPointsRecord.setCheckMonth(ym);
//                    fillPointsRecord.setRemark("存在"+propertyCompareList.size()+"产权数据没有登记");
//                    fillPointsRecord.setDeductScoreDept("系统");
//                    save(fillPointsRecord);
//                }
//                if(bankCompareList.size()>0){
//                    p.put("remainNum",bankCompareList.size());
//                    finalList.add(p);
//                    FillPointsRecord fillPointsRecord=new FillPointsRecord();
//                    fillPointsRecord.setType("3");
//                    fillPointsRecord.setScore(SpecialFormType.BANK_CAPITAL_DEDUCT_SCORE);
//                    fillPointsRecord.setOrgId(orgId);
//                    fillPointsRecord.setCreateTime(dateNow);
//                    fillPointsRecord.setModuleName("财务管理-银行资金管理");
//                    fillPointsRecord.setCheckMonth(ym);
//                    fillPointsRecord.setRemark("存在"+bankCompareList.size()+"家企业银行数据没有推送");
//                    fillPointsRecord.setDeductScoreDept("系统");
//                    save(fillPointsRecord);
//                }
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//    }
//
//    @Override
//    public List<FillAssessmentVO> getDeductScoreOrgList(FillPointsRecord fillPointsRecord) {
//        List<FillAssessmentVO> fillAssessmentVOList=new ArrayList<>();
//        ListResult listResult= orgzationService.findThirteen();
//        List<PageData> thirteenOrgList=listResult.getList();
//        List<PageData> noParentOrgList=new ArrayList<>();
//        String tokenOrgId=TokenUtil.getTokenOrgId();
//        Boolean flag=orgzationService.checkGzwName(tokenOrgId);
//        //登录人为国资委和集团区分权限
//        String startDate=fillPointsRecord.getCheckMonth()+"-01";
//        String endDate= DateUtil.getThisMonthlastdate(startDate);
//        QueryWrapper queryWrapper=new QueryWrapper();
//        queryWrapper.le("start_time",startDate+" 00:00:00");
//        queryWrapper.ge("end_time",endDate+" 23:59:59");
//        List<ModuleConfig> moduleConfigList=moduleConfigService.list(queryWrapper);
//        List<String> configOrgList=moduleConfigList.stream().map(ModuleConfig::getOrgId).distinct().collect(Collectors.toList());
//        if (CollectionUtil.isNotEmpty(configOrgList)){
//            List<MonitorResult> monitorResults=monitorResultService.getUseProperty(fillPointsRecord.getCheckMonth(),configOrgList);
//            Map<String,String> monitorMap=monitorResults.stream().collect(Collectors.toMap(MonitorResult::getOrgId,MonitorResult::getUseProperty));
//            if(!flag){
//                noParentOrgList.addAll(thirteenOrgList.stream().filter(record->record.getString("id").equals(tokenOrgId)).collect(Collectors.toList()));
//            }else{
//                for(PageData pOrg:thirteenOrgList){
//                    List<PageData> children= (List<PageData>) pOrg.get("children");
//                    if(children.size()>0){
//                        noParentOrgList.addAll(children);
//                    }else{
//                        noParentOrgList.add(pOrg);
//                    }
//                }
//            }
//            //只过滤模块配置过的扣分列表
//            noParentOrgList=noParentOrgList.stream().filter(record->configOrgList.contains(record.getString("id"))).collect(Collectors.toList());
//            Map<String,Object> param=new HashMap<>();
//            param.put("orgIds",configOrgList);
//            param.put("checkMonth",fillPointsRecord.getCheckMonth());
//            List<FillPointsRecord> fillPointsRecordList=fillPointsRecordMapper.selectSumDeductInfo(param);
//            Map<String,Integer> fillPointsRecordMap=fillPointsRecordList.stream().collect(Collectors.toMap(record->record.getOrgId()+"_"+record.getType(),record->record.getScore()));
//            for(PageData p:noParentOrgList){
//                FillAssessmentVO f=new FillAssessmentVO();
//                String orgId=p.getString("id");
//                f.setOrgName(p.getString("name"));
//                f.setOrgId(orgId);
//                if(monitorMap.containsKey(orgId)){
//                    f.setUseProperty(monitorMap.get(orgId));
//                }
//                if(fillPointsRecordMap.containsKey(orgId+"_1")){
//                    int value=fillPointsRecordMap.get(orgId+"_1");
//                    f.setAccuracyDeductScore(-1*value);
//                }else{
//                    f.setAccuracyDeductScore(0);
//                }
//                if(fillPointsRecordMap.containsKey(orgId+"_2")){
//                    int value=fillPointsRecordMap.get(orgId+"_2");
//                    f.setTimelinessDeductScore(-1*value);
//                }else{
//                    f.setTimelinessDeductScore(0);
//                }
//                if(fillPointsRecordMap.containsKey(orgId+"_3")){
//                    int value=fillPointsRecordMap.get(orgId+"_3");
//                    f.setIntegrityDeductScore(-1*value);
//                }else{
//                    f.setIntegrityDeductScore(0);
//                }
//                f.setSumDeductScore(f.getAccuracyDeductScore()+f.getTimelinessDeductScore()+f.getIntegrityDeductScore());
//                fillAssessmentVOList.add(f);
//            }
//        }
//        return fillAssessmentVOList;
//    }
//}
